﻿using System;
using System.Windows.Forms;

namespace Compiler.LabForms
{
    public partial class TokenEdit : Form
    {
        private ListView _tokensView;
        private ListViewItem _item;

        public TokenEdit(ListView tokensView)
        {
            _tokensView = tokensView;
            InitializeComponent();
            Text = @"Добавление токена";
            attributeBox.SelectedIndex = 0;
        }

        public TokenEdit(ListView tokensView, ListViewItem item)
        {
            _item = item;
            _tokensView = tokensView;
            InitializeComponent();
            Text = @"Редактирование токена";
            valueBox.Text = item.Text;
            attributeBox.SelectedIndex = item.ImageIndex;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(valueBox.Text)) return;
            if (_item != null)
            {
                _item.Text = valueBox.Text;
                _item.ToolTipText = getToolTipText(attributeBox.SelectedIndex);
                _item.ImageIndex = attributeBox.SelectedIndex;
            }
            else
            {
                var newItem = new ListViewItem
                {
                    Text = valueBox.Text,
                    ToolTipText = getToolTipText(attributeBox.SelectedIndex),
                    ImageIndex = attributeBox.SelectedIndex
                };
                _tokensView.Items.Add(newItem);
            }
            _tokensView.SelectedItems.Clear();
            Close();
        }

        private string getToolTipText(int imageIndex)
        {
            switch (imageIndex)
            {
                case 0 :
                    return "Константа";
                case 1:
                    return "Идентификатор";
                case 2:
                    return "Терминал";
                default:
                    return "";
            }
        }
    }
}
